<?php
namespace app\member\service;
use app\member\model\Configs;
use app\member\model\Users;
class UserService
{

    public function regUser($data){
        try{
            $config = new Configs();
            $state_id = $config->where("config_key",'=',"REG_STATE")->value("config_value");
            $group_id = $config->where("config_key",'=',"REG_GROUP")->value("config_value");
            $user = [];
            $user['username'] = $data['username'];
            $user['password'] = $data['password'];
            $user['nickname'] = $data['nickname'];
            $user['qq'] = $data['qq'];
            $user['bilibili_uid'] = $data['bilibili_uid'];
            $user['state_id'] = $state_id;
            $user['group_id'] = $group_id;
            $db = new Users();
            $isExist = $db->
            where("username",$user['username'])->
            where("is_delete",0)->
            find();
            if($isExist){
                $res = AjaxReturn(-1,"该账号已存在");
                return $res;
            }else{
                $db->insert($user);
                return AjaxReturn(0,"注册成功,请前往登录");
            }
        }catch (\Throwable $ex){
            $res = AjaxReturn(-1, $ex->getMessage());
            return $res;
        }
    }



    /**
     * Notes:验证登录
     * User: ZHY
     * DateTime: 2023/7/6 20:58
     * @param $username
     * @param $password
     * @return array
     */
    public function validateUser($username, $password)
    {
        try {
            $user = new Users();
            $where = [];
            array_push($where, ['username', '=', $username]);
            array_push($where, ['is_delete', '=', 0]);
            $f = $user->where($where)->find();
            if ($f) {
                if ($f['state'] == 0) {
                    $res = AjaxReturn(-1, '账号已禁用');
                } else {
                    if ($f['password'] == $password) {
                        session("adid",$f["id"]);
                        session("nickname",$f["nickname"]);
                        $res = AjaxReturn(0, '登录成功');
                    } else {
                        $res = AjaxReturn(-1, '密码错误');
                    }
                }
            } else {
                $res = AjaxReturn(-1, '账号不存在');
            }
            return $res;
        } catch (\Throwable $ex) {
            $res = AjaxReturn(-1, $ex->getMessage());
            return $res;
        }
    }

    /**
     * Notes:分页查询
     * User: ZHY
     * DateTime: 2023/7/5 21:02
     * @param $page
     * @param $limit
     * @param $where
     * @return array
     */
    public function list($page = 1, $limit = 10, $where = [])
    {
        try {
            array_push($where, ['users.is_delete', '=', 0]);
            $offset = ($page - 1) * $limit;
            $db = new Users();
            $li = $db->
            alias("a")->
            leftjoin("groups b",'a.group_id = b.id')->
            leftjoin("states c",'a.state_id = c.id')->
            where($where)->
            limit($offset, $limit)->
            order("users.id")->
            field("a.id,a.username,a.group_id,a.state_id,a.nickname,a.qq,a.bilibili_uid,b.group_name,c.state_name")->
            select();
            $count = $db->where($where)->count();
            return AjaxReturn(0, "", $li, $count);
        } catch (\Throwable $ex) {
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }

    /**
     * Notes:添加
     */
    public function add($data)
    {
        try {
            $db = new Users();
            $f = $db->where('username',$data['username'])->find();
            if($f){
                return AjaxReturn(-1, '添加失败,账号冲突');
            }

            $res = $db->insert($data);
            if ($res) {
                return AjaxReturn(0, '添加成功');
            } else {
                return AjaxReturn(-1, '添加失败');
            }
        } catch (\Throwable $ex) {
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }

    public function softdelete($ids)
    {
        try {
            $db = new Users();
            $res = $db->where('id', 'in', $ids)->update(['is_delete' => 1]);
            if ($res == count($ids)) {
                return AjaxReturn(0, '删除成功');
            } else {
                return AjaxReturn(-1, '删除失败');
            }
        } catch (\Throwable $ex) {
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }

    public function edit($data)
    {
        try {
            $db = new Users();
            $f = $db->where("username",$data['username'])->
                where("is_delete",0)->
                where("id",'<>',$data['id'])->find();
            if($f){
                return AjaxReturn(-1, '账号冲突');
            }
            $db->update($data);
            return AjaxReturn(0, '操作成功');
        } catch (\Throwable $ex) {
            return AjaxReturn(-1, '操作失败,错误信息:' . $ex->getMessage());
        }
    }




}